package com.wimcorp.magic.dataloader.abilities;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.wimcorp.magic.data.AbilityData;
import com.wimcorp.magic.input.AbilityInput;

public class RevealDeckAndChooseWidget extends AbilityWidget {

	private TextBox amount;
	private ListBox chosenDestinationList;
	private ListBox unChosenDestinationList;

	public RevealDeckAndChooseWidget(AbilityInput ability) {
		super(ability);
	}

	protected void initAbilityFields() {
		super.initAbilityFields();
		chosenDestinationList = new ListBox();
		unChosenDestinationList = new ListBox();
		amount = new TextBox();
		addInputSingleLocationList("Chosen destination", chosenDestinationList, 0);
		addInputSingleLocationList("Unchosen destination", unChosenDestinationList, 1);
		addInputTextBox("Amount", amount, 2);
	}

	public JSONObject getAbilityConfig() {
		JSONObject config = super.getAbilityConfig();
		config.put("chosenDestination", getSelectedItems(chosenDestinationList));
		config.put("unChosenDestination", getSelectedItems(unChosenDestinationList));
		config.put("amount", new JSONString(amount.getValue()));
		return config;
	}

	public void load(AbilityData abilityData) {
		super.load(abilityData);
		JSONObject config = (JSONObject) JSONParser.parseLenient(abilityData.getConfig());

		loadListBox(chosenDestinationList, config.get("chosenDestination"));

		loadListBox(unChosenDestinationList, config.get("unChosenDestination"));

		JSONValue value = config.get("amount");
		if (value != null) {
			amount.setValue(toString(value));
		}
	}

}
